import {UnwrapNestedRefs} from "@vue/reactivity";
import {
    useAfterSaleConfigStore
} from "@/base/stores/modules/afterSaleConfig";
import {AfterSaleConfigItemDTO} from "@/api/system/dto/ AfterSaleConfigItemDTO";
import {AddressTreeDTO} from "@/api/system/dto/AddressTreeDTO";
import {AfterSaleOrderEnumItemDTO} from "@/api/system/dto/AfterSaleStatusItemDTO";
import {ItemLineInfoDTO} from "@/api/afterSale/order/dto/ItemLineInfoDTO";


const afterSaleConfigStore = useAfterSaleConfigStore();


/**
 *
 * 售后配置信息
 *
 * @author 徐寿春  AfterSaleOrderConfigTypeEnum
 * 2023/4/12 10:27
 */
export class AfterSaleEnumConfigManager {

    itemLineInfos: ItemLineInfoDTO [] = []

    orderChannelConfig: AfterSaleConfigItemDTO [] = []
    orderShopConfig: AfterSaleConfigItemDTO [] = []
    orderSourceConfig: AfterSaleConfigItemDTO [] = []
    orderCauseConfig: AfterSaleConfigItemDTO [] = []
    orderWarehouseConfig: AfterSaleConfigItemDTO [] = []


    materialFailureCauseConfig: AfterSaleConfigItemDTO [] = []    // 质检实况
    materialSituationConfig: AfterSaleConfigItemDTO [] = []    // 不通过原因
    cooperateProcessMode: AfterSaleConfigItemDTO [] = [] // 协同处理方式

    addressTree: AddressTreeDTO [] = []

    afterSaleOrderType: AfterSaleOrderEnumItemDTO[] = []
    cooperateOrderType: AfterSaleOrderEnumItemDTO[] = []
    shippingOrderType: AfterSaleOrderEnumItemDTO[] = []

    afterSaleOrderStatus: AfterSaleOrderEnumItemDTO[] = []
    afterSaleOrderLineStatus: AfterSaleOrderEnumItemDTO [] = []
    cooperateOrderStatus: AfterSaleOrderEnumItemDTO [] = []
    cooperateOrderLineStatus: AfterSaleOrderEnumItemDTO [] = []
    warehouseOrderStatus: AfterSaleOrderEnumItemDTO [] = []
    afterSaleShippingOrderStatus: AfterSaleOrderEnumItemDTO [] = []

}


/**
 * 售后平台枚举
 */
export function useAfterSaleEnumManager() {
    let afterSaleOrderConfig: UnwrapNestedRefs<AfterSaleEnumConfigManager> = reactive(new AfterSaleEnumConfigManager())


    afterSaleConfigStore.getAfterSaleConfig().then(rsp => {
        // 读取其他
        for (let afterSaleOrderConfigKey in afterSaleOrderConfig) {
            // @ts-ignore
            afterSaleOrderConfig[afterSaleOrderConfigKey] = rsp[afterSaleOrderConfigKey]
        }

        // 读取工单状态
        for (let key in rsp.afterSaleOrderEnumInfo) {
            // @ts-ignore
            afterSaleOrderConfig[key] = rsp.afterSaleOrderEnumInfo[key]
        }

        // 读取配置
        for (let key in rsp.afterSaleConfigInfo) {
            // @ts-ignore
            afterSaleOrderConfig[key] = rsp.afterSaleConfigInfo[key]
        }

        // 计算店铺
        let arr: AfterSaleConfigItemDTO[] = []
        rsp.afterSaleConfigInfo.orderChannelConfig.filter(item => item.children).forEach(item => {
            arr.push(...item.children)
        })
        afterSaleOrderConfig.orderShopConfig = arr


    })

    return afterSaleOrderConfig;

}

